package com.boyinet.activation.controller;

import com.boyinet.activation.entity.Permission;
import com.boyinet.activation.entity.User;
import com.boyinet.activation.service.PermissionService;
import com.boyinet.activation.service.UserService;
import com.boyinet.activation.util.PermissionHelper;
import com.boyinet.activation.util.R;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lengchunyun
 */
@RequestMapping("app")
@RestController
public class AppController {

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private UserService userService;

    @GetMapping()
    private R app() {

        List<Permission> list = permissionService.list();
        List<Permission> menu = PermissionHelper.bulid(list);
        Map<String, Object> data = new HashMap<>(4);
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        User user = userService.getUserByName(name);
        user.setPassword(null);
        data.put("menu", menu);
        data.put("app", new Project("ng-alain"));
        data.put("user", user);
        return R.ok().put("data", data);
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    static class Project {
        private String name;
    }
}
